﻿using System.Windows.Input;
using CharacterCreation.Data.Models;
using CharacterCreation.Data.Services;
using CharacterCreation.Views.ChildWindows;
using CharacterCreation.Views.Contracts;
using Microsoft.Practices.Prism.Commands;

namespace CharacterCreation.Views {
    /// <summary>
    /// The viewmodel 
    /// </summary>
    public class CharacterEditSkillsViewModel : BaseViewModel {
        private CharacterService _characterService = CharacterService.CreateInstance();

        /// <summary>
        /// The new character
        /// </summary>
        public Character Character { get { return _characterService.CurrentItem; } }

        /// <summary>
        /// Gets or sets the SelectedCharacterSkill
        /// </summary>
        public CharacterSkill SelectedCharacterSkill {
            get { return _SelectedCharacterSkill; }
            set {
                _SelectedCharacterSkill = value;
                RaisePropertyChanged("SelectedCharacterSkill");
            }
        }
        private CharacterSkill _SelectedCharacterSkill;

        /// <summary>
        /// Add a characterskill to a character
        /// </summary>
        public ICommand AddCommand {
            get {
                return new DelegateCommand(() => {
                    var skillService = SkillService.CreateInstance();
                    skillService.Clear();
                    PopupWindow popupWindow = new PopupWindow(
                        new BaseView[] { new SkillSelectionListView() }
                    );
                    popupWindow.Closing += (s, e) => {
                        if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                            this.Character.Skills.Add(new CharacterSkill() { Skill = skillService.CurrentItem });
                        }
                    };
                    popupWindow.ShowDialog();
                });
            }
        }

        /// <summary>
        /// Remove a character skill
        /// </summary>
        public ICommand DeleteCommand {
            get {
                return new DelegateCommand(
                    () => {
                        if (this.SelectedCharacterSkill != null)
                            this.Character.Skills.Remove(SelectedCharacterSkill);
                    });
            }
        }
        public CharacterEditSkillsViewModel() {
        }
    }
}
